package com.freeman.cement.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.freeman.cement.entity.ResponseResult;
import com.freeman.cement.exception.AppException;
import com.freeman.cement.exception.AppExceptionCodeMsg;
import com.freeman.cement.mapper.PhoneRecordDao;
import com.freeman.cement.entity.PhoneRecord;
import com.freeman.cement.service.PhoneService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PhoneServiceImpl implements PhoneService {

    private static Logger logger = LoggerFactory.getLogger(PhoneServiceImpl.class);

    @Resource
    private PhoneRecordDao dao;

    @Override
    public ResponseResult selectPhoneRecordByEventId(String eventId, int pageNo, int pageSize) {
        //验证分页范围
        if (pageNo < 1 || pageSize > 1000) {
            throw new AppException(AppExceptionCodeMsg.PAGE_NO_OR_SIZE_WRONG);
        }
        if(StringUtils.isBlank(eventId)){
            throw new AppException(AppExceptionCodeMsg.ID_IS_NULL);
        }

        PageHelper.startPage(pageNo, pageSize, true);
        List<PhoneRecord> list = dao.selectPhoneRecordByEventId(eventId);
        PageInfo<PhoneRecord> pageInfo = new PageInfo<PhoneRecord>(list);
        return ResponseResult.success(pageInfo);
    }

    @Override
    public void addPhoneRecord(PhoneRecord record) {
        if (record != null) {
            if(!(dao.addPhoneRecord(record) > 0)){
                logger.error("addPhoneRecord--->" + "[phonerecord]插入数据失败:" + record.toString());
            }
        }
    }

    @Override
    public void updatePhoneRecord(PhoneRecord record) {
        if(record != null) {
           if(!(dao.updatePhoneRecord(record) > 0)){
               logger.error("updatePhoneRecord--->" + "[phonerecord]修改数据失败:" + record.toString());
           }
        }
    }

}
